import { userList, userCount } from '@/request/app'
interface adminState {
    adminList: Array<object>
    total: number
    page: number
    size: number
}
const state = {
    adminList: [],
    size: 2, // 条数
    page: 1, // 页数
    total: 0  //总数
}
const getters = {
    // 列表
    getAdminList(state: adminState) {
        return state.adminList
    },
    // 总数
    getTotal(state: adminState) {
        return state.total
    },
    // 条数
    getSize(state: adminState) {
        return state.size
    }
}
const mutations = {
    // 列表
    REQ_ADMIN(state: adminState, payload: any) {
        state.adminList = payload
    },
    // 总数
    REQ_TOTAL(state: adminState, payload: number) {
        state.total = payload
    },
    // 改变页码
    CHANGE_PAGE(state: adminState, payload: number) {
        state.page = payload
    }
}
const actions = {
    // 管理员列表
    adminListAction(context: any) {
        console.log(context, 'context');
        userList({
            size: context.state.size,
            page: context.state.page,
        })
            .then((res: any) => {
                console.log(res, '管理员列表');
                if (res.code == 200) {
                    let list = res.list ? res.list : []
                    context.commit('REQ_ADMIN',list)
                }
            })
    },
    // 总数
    adminTotalAction({ commit }: any) {
        userCount()
            .then((res: any) => {
                console.log(res.list[0].total, '总数');
                if(res.code == 200){
                    commit('REQ_TOTAL', res.list[0].total)
                }
                
            })
    },
    // //封装一个切换页码的行动
    changePageAction(context: any, payload: number) {
        // 修改page
        context.commit('CHANGE_PAGE', payload)
        // 重新再调用列表行动
        context.dispatch('adminListAction')

    }
}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}